<?php
/**
 * @Author    紫梦
 * @DateTime  2018-02-09
 * @copyright 响响租车
 * @license   [license]
 * @version   1.0
 * @param     string     $value [description]
 */
namespace app\index\controller;
use think\Request;
use app\common\request\ClientLai;
use app\common\request\Curl;
use app\common\model\Cars;
use app\common\controller\Operate as Action;
use app\common\controller\Gps;

class Operate extends Base
{
    
    public function index($id)
    {
        
        $cars = Cars::where('car_id',$id)->field('car_unique_id,car_sn,supplier_no,cardcode,equipment_id')->find();
        if($cars['supplier_no'] == 1376920002){
            $config = config('gz_blt');
            $obj = new ClientLai($config['user_name'],$config['password']);
            $res = $obj->request_post(rtrim($config['url'],'/').'/api/vehicle/carData/getRtInfo',http_build_query(['car_unique_id'=>$cars['car_unique_id']]));
            $arr = json_decode($res,true);
            if($arr['result'] == 0){
                if(empty($arr['data'][0])){
                    $data = null;
                }else{
                    $cars_data = $arr['data'][0];
                    $cars_data['long_term_lease'] = $cars_data['supplier_rent_type'];
                    $data = $cars_data; 
                }
            }else{
                $data = null;
            }

        }elseif($cars['supplier_no'] == 1376920003){
            $config = config('local_iot');
            $obj = new ClientLai($config['user_name'],$config['password']);
            $res = $obj->request_post(rtrim($config['url'],'/').'/car/getRtInfo',http_build_query(['car_unique_id'=>$cars['car_unique_id']]));
            $arr = json_decode($res,true);
            if($arr['result'] == 0){
                if(empty($arr['data'][0])){
                    $data = null;  
                }else{
                    $data = $arr['data'][0];
                    
                }
            }else{
                $data = null;
            }

        }else{
            $data = null;
        }
        if($data){
            $result = Gps::geocode($data['longitude'],$data['latitude']);
            if($result){
                $gaodeapi = json_decode($result,true);            
            }else{
                $gaodeapi = null;
            }  
        }else{
            $gaodeapi = null;
        }
        $this->assign('data',$data);
        $this->assign('cars',$cars);
        $this->assign('gaodeapi',$gaodeapi);
        $this->assign('id',$id);
        return $this->fetch();
        
    }

    public static function geocode($lng,$lat)
    {   
        $config = config('ali_api');
        $path = "/v3/geocode/regeo";
        $transgps = self::transgps($config['appcode'],$lat,$lng);
        if($transgps){
            $transgps = json_decode($transgps,true);
            $querys = "location=".$transgps[0]['Lng'].','.$transgps[0]['Lat'];
            $url = 'http://regeo.market.alicloudapi.com' . $path . "?" . $querys;
            $curl = new Curl; 
            $res = $curl->set('CURLOPT_HTTPHEADER',["Authorization:APPCODE " . $config['appcode']])->url($url);

            // 任务结果状态
            if ($curl->error()) {
                $curl->message();
            } else {
                // 任务进程信息
                $info = $curl->info();                
                if($info['http_code'] == 200){
                    // 任务结果内容
                    return $curl->data();
                }
            }           
        }

        return false;
    }

    public static function transgps($appcode,$lat,$lng){
        $host = "http://gps.eidagent.com";
        $path = "/transgps";
        $querys = "lats={$lat}&lngs={$lng}&type=1";
        $url = $host . $path . "?" . $querys;
        $curl = new Curl; 
        $res = $curl->set('CURLOPT_HTTPHEADER',["Authorization:APPCODE " . $appcode])->url($url);

        // 任务结果状态
        if ($curl->error()) {
            $curl->message();
        } else {
            // 任务进程信息
            $info = $curl->info();            
            if($info['http_code'] == 200){
                return  $curl->data();
            }
        }
    }

    public function unlock($id)
    {
        $model = Cars::get($id);

        if($model['supplier_no'] == 1376920002){

            $config = config('gz_blt');

            $url = rtrim($config['url'],'/').'/api/vehicle/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'LOCK',1,$url);

        }elseif ($model['supplier_no'] == 1376920003) {

            $config = config('local_iot');

            $url = rtrim($config['url'],'/').'/command/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'LOCK',1,$url);
        }
    }

    public function lock($id)
    {
        $model = Cars::get($id);

        if($model['supplier_no'] == 1376920002){

            $config = config('gz_blt');

            $url = rtrim($config['url'],'/').'/api/vehicle/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'LOCK',0,$url);

        }elseif ($model['supplier_no'] == 1376920003) {

            $config = config('local_iot');

            $url = rtrim($config['url'],'/').'/command/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'LOCK',0,$url);
        }
    }

    public function ignite($id)
    {
        $model = Cars::get($id);

        if($model['supplier_no'] == 1376920002){

            $config = config('gz_blt');

            $url = rtrim($config['url'],'/').'/api/vehicle/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'IGNITE',1,$url);

        }elseif ($model['supplier_no'] == 1376920003) {

            $config = config('local_iot');

            $url = rtrim($config['url'],'/').'/command/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'IGNITE',1,$url);
        }
    }

    public function unignite($id)
    {
        $model = Cars::get($id);
        
        if($model['supplier_no'] == 1376920002){

            $config = config('gz_blt');

            $url = rtrim($config['url'],'/').'/api/vehicle/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'IGNITE',0,$url);

        }elseif ($model['supplier_no'] == 1376920003) {

            $config = config('local_iot');

            $url = rtrim($config['url'],'/').'/command/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'IGNITE',0,$url);
        }
    }

    public function whistle($id)
    {
        $model = Cars::get($id);
        
        if($model['supplier_no'] == 1376920002){

            $config = config('gz_blt');

            $url = rtrim($config['url'],'/').'/api/vehicle/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'WHISTLE',1,$url);

        }elseif ($model['supplier_no'] == 1376920003) {

            $config = config('local_iot');

            $url = rtrim($config['url'],'/').'/command/doCommandAction';

            return Action::operation($config,$model['car_unique_id'],'WHISTLE',1,$url);
        }
    }

    public function rent_type($car_unique_id = 0,$type = 1){
        $data['car_unique_id'] = $car_unique_id;
        $data['type'] = $type;
       
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $res = $obj->request_post(rtrim($config['url'],'/').'/command/doNotifyRentType',$data);
        $arr = json_decode($res,true);
        if($arr['result']==0){
            return [
                'result'=>0,
                'msg'=>'切换成功'
            ];
        }else{
            return [
                'result'=>2,
                'msg'=>$arr['msg']
            ];
        }
    }

    public function photographer($car_unique_id = 0){
        $data['car_unique_id'] = $car_unique_id;
        $data['seq_id'] = md5(time());
        $data['time'] = time()*1000;
        $config = config('local_iot');
        $obj = new ClientLai($config['user_name'],$config['password']);
        $res = $obj->request_post(rtrim($config['url'],'/').'/command/doTakePhoto',http_build_query($data));
        $arr = json_decode($res,true);
        if($arr['result']==0){
            return [
                'result'=>0,
                'msg'=>'指令执行成功'
            ];
        }else{
            return [
                'result'=>2,
                'msg'=>$arr['msg']
            ];
        }
    }

    public function switch($car_unique_id = 0,$type = 1){
        $request = Request::instance();
        if($request->isAjax()){

            $data['car_unique_id'] = $car_unique_id;
            $data['type'] = $type;
            $config = config('local_iot');
            $obj = new ClientLai($config['user_name'],$config['password']);
            $res = $obj->request_post(rtrim($config['url'],'/').'/command/doNotifyRentType',$data);
            $arr = json_decode($res,true);
            if($arr['result']==0){
                return [
                    'result'=>0,
                    'msg'=>'切换成功'
                ];
            }else{
                return [
                    'result'=>2,
                    'msg'=>$arr['msg']
                ];
            }

        }else{

            $this->assign('cars',$car_unique_id);
            return $this->fetch();
        }   
    }
    
}